package qiyu.im.core.server.constants;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * Created with IntelliJ IDEA.
 * Description: IM的编码器实现,也就是服务端发送消息的时候,把消息按照imMsg的格式写出去
 * User: sfx
 * Date: 2023-10-16
 * Time: 18:12
 */
public class ImEncoder extends MessageToByteEncoder {
    @Override
    protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception {
        ImMsg imMsg = (ImMsg) msg;
        out.writeShort(imMsg.getMagic());
        out.writeInt(imMsg.getLen());
        out.writeInt(imMsg.getCode());
        out.writeBytes(imMsg.getBody());
        ctx.writeAndFlush(imMsg);
    }
}
